package com.liang.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.pojo.Data;
import com.liang.pojo.Page;
import com.liang.pojo.User;
import com.liang.service.PageService;
import com.liang.service.UserService;

@Controller
public class PageController {
	@Autowired
	PageService pageService;
	@Autowired
	UserService userService;

	/*
	 * @RequestMapping("/page.do") public String pageList(@RequestParam(value =
	 * "currentPage",required = false,defaultValue = "1")int currentPage,
	 * 
	 * @RequestParam(value = "pageSize",required = false,defaultValue = "10")int
	 * pageSize,Model model) { Page page = pageService.getPage(currentPage,
	 * pageSize); model.addAttribute("page", page); return "page"; }
	 * 
	 * @RequestMapping("/page2.do") public String pageList3() { return "page2"; }
	 * 
	 * 
	 * @RequestMapping("/page3.do")
	 * 
	 * @ResponseBody public Object pageList2(@RequestParam(value =
	 * "currentPage",required = false,defaultValue = "1")int currentPage,
	 * 
	 * @RequestParam(value = "pageSize",required = false,defaultValue = "10")int
	 * pageSize) { Page page = pageService.getPage(currentPage, pageSize); return
	 * page; }
	 */
	@RequestMapping("/page3.do")
	public String pageList3() {
		return "page3";
	}
	@RequestMapping("/page5")
	public String pageList5() {
		return "page5";
	}
	@RequestMapping("/add.htm")
	public String add() {
		return "toAdd";
	}
	
	 @RequestMapping("/showUser.do")
	    public String getUsers(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
	        //从第一条开始 每页查询五条数据
	     PageHelper.startPage(pn, 12);
	        List<User> users = pageService.selectAllUser();
	        System.out.println(users.size());
	        //将用户信息放入PageInfo对象里
	        PageInfo page = new PageInfo(users);
	        model.addAttribute("pageInfo", page);
	        model.addAttribute("userList",users);
	        return "page4";
	    }
	 @ResponseBody
	 @RequestMapping("/showUser2.do")
	 public Object getUsers2(@RequestParam(value="pn",defaultValue="1")Integer pn,
			 @RequestParam(value = "p",required = false)String p){
		 //从第一条开始 每页查询五条数据
		 PageHelper.startPage(pn, 12);
		 Map<String,Object> map=new HashMap<String,Object>();
		 if(p==null||"".equals(p)) {
			 List<User> users = pageService.selectAllUser2(map);
			 PageInfo page = new PageInfo(users);
			 return page;
		 }else {
			 map.put("p", p);
			 List<User> users = pageService.selectAllUser2(map);
			 System.out.println(users);
			 PageInfo page = new PageInfo(users);
			 return page;
		 }
		 
	 }
	@RequestMapping("/page3.doo")
	@ResponseBody
	public Object pageList3(@RequestParam(value = "currentPage",required = false,defaultValue = "1")int currentPage,
			@RequestParam(value = "pageSize",required = false,defaultValue = "10")int pageSize,
			@RequestParam(value = "pageText",required = false)String pageText) {
		System.out.println("==================="+pageText);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("currentPage", currentPage);
		map.put("pageSize", pageSize);
		if(pageText==null || "".equals(pageText)) {
			Page page2 = pageService.getPage2(map);
			return page2;
		}
		if(pageText.contains("%")) {
			pageText=pageText.replaceAll("%", "\\\\%");
		}
		map.put("pageText", pageText);
		Page page2=pageService.getPage2(map);
		return page2;
		
	}
	
	@RequestMapping("/add.do")
	public String add2(User user) {
		pageService.insertUser(user);
		return "page3";
	}
	@RequestMapping("/delete.do")
	public String delete(Page page) {
		/*
		 * for(int id:ids) { pageService.deleteUser(id); }
		 */
		pageService.deleteBeach(page);
		return "page3";
	}
	
	@RequestMapping("/toUpdate.do/{id}")
	public String toUpdate(@PathVariable("id")int id,Model model) {
		User user=pageService.selectUserById(id);
		model.addAttribute("user", user);
		return "toUpdateUser";
	}
	@ResponseBody
	@RequestMapping("/update.do")
	public int update(User user) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id", user.getId());
		map.put("username", user.getUsername());
		map.put("password", user.getPassword());
		map.put("permissions", user.getPermissions());
		int i=pageService.updateUser(map);
		return i;
	}

	/*
	 * @ResponseBody
	 * 
	 * @RequestMapping("/delete.doo") public int deleteUser(@RequestParam("id")int
	 * id) { int i=pageService.deleteUserById(id); return i; }
	 */
	@ResponseBody
	@RequestMapping("/delete.dooo")
	public int deleteUsero(Data data) {
		
		int i=pageService.deleteUserByList(data);
		return i;
	}
	
	@RequestMapping("/toInsertUser")
	public String toInsertUser() {
		return "toInsertUser";
	}
	@ResponseBody
	@RequestMapping("/insertUser")
	public int insertUser(User user) {
		int i = pageService.insertUser(user);
		return i;
	}
	
	
	
	
	
	
	
	/*
	 * @ResponseBody
	 * 
	 * @RequestMapping("") public Object allRole(int currentUserId,Model model) {
	 * List rightList=new ArrayList<Role>(); List leftList=new ArrayList<Role>();
	 * //数据库查询全部角色返回list for(Role role:list) { if(currentUserId==role.id) {
	 * rightList.add(role); }else { leftList.add(role); } } //数据库查询当前用户拥有的角色list
	 * model.addAttribute("current, attributeValue) return null; }
	 */
}
